<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="1"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher @close="dealercloseHandler"/>
    </BaseDialog>

    <BaseDialog
      :visible.sync="selectCentral"
      :title="'仓库查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <selectCentral @close="centralcloseHandler"/>
    </BaseDialog>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import {dictionary} from '@/utils/dictMixin';
import {
  addStorehouseDealer,
  updateStorehouseDealer
} from '@/api/partsmanage/partWarehouseManage/storehouseDealer/index';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import selectCentral from './selectCentral';

const appId = localStorage.getItem('appId');

export default {
  name: "updateStorehouse",
  mixins: [dictionary],
  components: {ownerSeacher, selectCentral},
  props: {
    type: {
      type: String,
      default: 'add',
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      fetchApi: null,
      selectCentral: false,
      issearch: false,
      saveLoading: false,
      bsckid: '',
      fetchParams: null,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.info(this.type)
      console.info(this.rowData)
      if (this.type == 'edit') {
        this.bsckid = this.rowData.bsckid
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '油电属性',
          fieldName: 'bjpjsx',
          itemList: this.createDictList(5000),
          disabled: appId == 'arcfox',
          initialValue: appId == 'arcfox' ? 50001002 : '',
          rules: [
            {required: true, message: '请选择油电属性', trigger: 'blur'},
          ]
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'jxcode',
          placeholder: '请选择',
          readonly: true,
          initialValue: '',
          rules: [{required: true, message: '请选择经销商代码', trigger: 'blur'}],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.dealerCode = ''
            }
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'jxname',
          maxlength: 50,
          disabled: true,
          initialValue: '',
          rules: [{required: true, message: '请输入经销商名称', trigger: 'blur'}]
        },
        {
          type: 'INPUT',
          label: '仓库代码',
          fieldName: 'bsckdm',
          placeholder: '请选择',
          readonly: true,
          initialValue: '',
          rules: [{required: true, message: '请选择仓库代码', trigger: 'blur'}],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
        },
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'bsckmc',
          maxlength: 50,
          disabled: true,
          initialValue: '',
          rules: [{required: true, message: '请输入仓库名称', trigger: 'blur'}]
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'xtqybz',
          itemList: this.createDictList('1003'),
          disabled: true,
          initialValue: 10031001,
        }
      ]
    },
    formChangeHandle(val) {
      console.info(val)
      this.saveData = val;
    },
    async saveDialog() {
      const err = this.$refs.formPanel.SUBMIT_FORM();
      if (err === null) {
        return false
      }
      this.saveLoading = true;
      this.saveData.bsckid = this.bsckid
      let saveApi;
      if (this.type === 'edit') {
        this.saveData.id = this.rowData.id;
        saveApi = updateStorehouseDealer;
      } else {
        saveApi = addStorehouseDealer;
      }
      console.info('this.saveData', this.saveData);
      const res = await saveApi(this.saveData);
      if (res.resultCode === 200) {
        this.closeDialog('refresh');
      }
      this.saveLoading = false
    },
    seacherFun(type) {
      if (type == 1) {
        this.issearch = true
      } else {
        this.selectCentral = true
      }
    },
    dealercloseHandler(val) {
      console.info(val)
      if (val) {
        console.info('1111111111')
        this.formList.find(x => x.fieldName == 'jxcode').initialValue = val.dealerCode;
        this.formList.find(x => x.fieldName == 'jxname').initialValue = val.dealerName;
      }
      this.issearch = false;
    },
    centralcloseHandler(val) {
      if (val) {
        this.formList.find(x => x.fieldName == 'bsckdm').initialValue = val.bsckdm;
        this.formList.find(x => x.fieldName == 'bsckmc').initialValue = val.bsckmc;
        this.bsckid = val.id
      }
      this.selectCentral = false;
    },
    closeDialog(param) {
      this.$emit('close', {visible: false, type: param});
    }
  }
}
</script>

<style scoped>

</style>
